<?php

include_once 'constants.php';

/**
 * Tell WordPress to run twentyeleven_setup() when the 'after_setup_theme' hook is run.
 */
add_action('after_setup_theme', 'bag_setup');
if (!function_exists('bag_setup')):

    /**
     * Sets up theme defaults and registers support for various WordPress features.
     *
     * Note that this function is hooked into the after_setup_theme hook, which runs
     * before the init hook. The init hook is too late for some features, such as indicating
     * support post thumbnails.
     * 
     */
    function bag_setup() {

        /* Make 1morebag available for translation.
         * Translations can be added to the /languages/ directory.
         */
        load_theme_textdomain('morebag', get_template_directory() . '/languages');

        // This theme styles the visual editor with editor-style.css to match the theme style.
        add_editor_style();

        // Add default posts and comments RSS feed links to <head>.
        add_theme_support('automatic-feed-links');

        // This theme uses wp_nav_menu() in one location.
        register_nav_menu('primary', __('Primary Menu', 'morebag'));

        // Add support for a variety of post formats
        add_theme_support('post-formats', array('aside', 'link', 'gallery', 'status', 'quote', 'image'));

        // This theme uses Featured Images (also known as post thumbnails) for per-post/per-page Custom Header images
        add_theme_support('post-thumbnails');

        // Use for home page slide show
        add_image_size('slide-show', 700, 320, true);
    }

endif;

/**
 * Sets the post excerpt length to 40 words.
 *
 * To override this length in a child theme, remove the filter and add your own
 * function tied to the excerpt_length filter hook.
 */
function morebag_excerpt_length($length) {
    return 40;
}

add_filter('excerpt_length', 'morebag_excerpt_length');

/**
 * Returns a "Continue Reading" link for excerpts
 */
function morebag_continue_reading_link() {
    return ' <a href="' . esc_url(get_permalink()) . '">' . __('Continue reading <span class="meta-nav">&rarr;</span>', 'twentyeleven') . '</a>';
}

/**
 * Replaces "[...]" (appended to automatically generated excerpts) with an ellipsis and twentyeleven_continue_reading_link().
 *
 * To override this in a child theme, remove the filter and add your own
 * function tied to the excerpt_more filter hook.
 */
function morebag_auto_excerpt_more($more) {
    return ' &hellip;' . morebag_continue_reading_link();
}

add_filter('excerpt_more', 'morebag_auto_excerpt_more');

/**
 * Get our wp_nav_menu() fallback, wp_page_menu(), to show a home link.
 */
function morebag_page_menu_args($args) {
    $args['show_home'] = true;
    return $args;
}

add_filter('wp_page_menu_args', 'morebag_page_menu_args');

/**
 * Register our sidebars and widgetized areas. Also register the default Epherma widget.
 *
 * @since One More Bag 1.0
 */
function morebag_widgets_init() {

    //register_widget( 'More_Bage_Ephemera_Widget' );

    register_sidebar(array(
        'name' => __('Top Right Sidebar Ads', 'morebag'),
        'id' => 'top-right-sidebar-ads',
        'before_widget' => '<div id="%1$s" class="widget %2$s">',
        'after_widget' => "</div>",
        'before_title' => '<h3 class="widget-title">',
        'after_title' => '</h3>',
    ));

    register_sidebar(array(
        'name' => __('Second Right Sidebar Ads', 'morebag'),
        'id' => 'sidebar-2',
        'description' => __('The sidebar for the second ads', 'morebag'),
        'before_widget' => '<aside id="%1$s" class="widget %2$s">',
        'after_widget' => "</aside>",
        'before_title' => '<h3 class="widget-title">',
        'after_title' => '</h3>',
    ));

    register_sidebar(array(
        'name' => __('Footer Area One', 'morebag'),
        'id' => 'sidebar-3',
        'description' => __('An optional widget area for your site footer', 'morebag'),
        'before_widget' => '<aside id="%1$s" class="widget %2$s">',
        'after_widget' => "</aside>",
        'before_title' => '<h3 class="widget-title">',
        'after_title' => '</h3>',
    ));

    register_sidebar(array(
        'name' => __('Footer Area Two', 'morebag'),
        'id' => 'sidebar-4',
        'description' => __('An optional widget area for your site footer', 'morebag'),
        'before_widget' => '<aside id="%1$s" class="widget %2$s">',
        'after_widget' => "</aside>",
        'before_title' => '<h3 class="widget-title">',
        'after_title' => '</h3>',
    ));

    register_sidebar(array(
        'name' => __('Footer Area Three', 'morebag'),
        'id' => 'sidebar-5',
        'description' => __('An optional widget area for your site footer', 'morebag'),
        'before_widget' => '<aside id="%1$s" class="widget %2$s">',
        'after_widget' => "</aside>",
        'before_title' => '<h3 class="widget-title">',
        'after_title' => '</h3>',
    ));
}

add_action('widgets_init', 'morebag_widgets_init');

/**
 * WP E-Commerce Hook
 *  
 */
function morebag_wpsc_products_shortcode($atts) {
    $number_per_page = get_option('use_pagination') ? get_option('wpsc_products_per_page') : 0;
    $query = shortcode_atts(array(
        'category_id' => isset($atts['cat']) ? $atts['cat'] : 0,
        'limit_of_items' => 0,
        'sort_order' => null,
        'number_per_page' => $number_per_page,
        'page' => 0,
            ), $atts);

    return wpsc_display_products_page($query);
}

add_shortcode('morebag_wpsc_category', 'morebag_wpsc_products_shortcode');

function morebag_wpsc_products_category($cat_ID) {
    return wpsc_display_products_page(array('category_id' => $cat_ID, 'number_per_page' => 6));
}

add_action('init', 'wpec_genesis_add_layout_support');

function wpec_genesis_add_layout_support() {
    add_post_type_support('wpsc-product', 'genesis-layouts');
}

add_action('init', 'filter_wpsc_script', 5);

function filter_wpsc_script() {
    if (function_exists('wpsc_the_product')) {
       // remove_action('init', 'wpsc_enqueue_user_script_and_css');
    }
}

if (!is_admin()) {
    if (function_exists('wpsc_the_product')) { // this checks to see if WPEC is even active 
        add_action( 'wp_print_styles', 'mb_deregister_styles', 100 );
    }
}
 
function mb_deregister_styles() {
        wp_deregister_style( 'wpsc-theme-css' );
	wp_deregister_style( 'wpsc-theme-css-compatibility' );
	wp_deregister_style( 'wpsc-product-rater' );
	wp_deregister_style( 'wp-e-commerce-dynamic');
	//wp_deregister_style( 'wpsc-thickbox' );
	wp_deregister_style( 'wpsc-gold-cart' );
}
 
if (!is_admin()) {
    if (function_exists('wpsc_the_product')) { // this checks to see if WPEC is even active 
        add_action( 'wp_print_scripts', 'mb_deregister_javascript', 100 );
    }
}
 
function mb_deregister_javascript() {
        //wp_deregister_script( 'wpsc-thickbox' );
	//wp_deregister_script( 'jquery-rating' );
	//wp_deregister_script( 'wp-e-commerce' );
	//wp_deregister_script( 'jQuery' );
	//wp_deregister_script( 'infieldlabel' );
	//wp_deregister_script( 'wp-e-commerce-ajax-legacy' );
	//wp_deregister_script( 'wp-e-commerce-dynamic' );
	//wp_deregister_script( 'livequery' );
	//wp_deregister_script( 'wp-e-commerce-legacy' );
	//wp_deregister_script( 'l10n' );
	//wp_deregister_script( 'wpsc-gold-cart' );
}

// custom function to get the full size product image
if (!function_exists('mb_wpsc_the_product_image')) {

    function mb_wpsc_the_product_image($product_id = '') {
        if (empty($product_id))
            $product_id = get_the_ID();

        $product = get_post($product_id);

        if ($product->post_parent > 0)
            $product_id = $product->post_parent;

        $attached_images = (array) get_posts(array(
                    'post_type' => 'attachment',
                    'numberposts' => 1,
                    'post_status' => null,
                    'post_parent' => $product_id,
                    'orderby' => 'menu_order',
                    'order' => 'ASC'
                ));

        $attached_image = wp_get_attachment_image_src($attached_images[0]->ID, 'full');

        $image_src = $attached_image[0];

        // run image url string check for multisite
        //$image_src = sp_check_ms_image( $image_src );

        return $image_src;
    }

}

// remove "from" on pricing
function wpsc_remove_from_for_variations($from) {
    return '';
}
 
add_filter('wpsc_product_variation_text','wpsc_remove_from_for_variations');


?>
